
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>字符串分割性能对比 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="对于开发人员来说，字符串分割很常见。那么我们就来掰一下他的由来和应用场景，在前后端交互场景中，用户批量操作， 比如批量审批退款，批量删除数据等等，这对于程序来说就是一种批量操作，这种问题有两种解决方案：">
<meta property="og:type" content="article">
<meta property="og:title" content="字符串分割性能对比">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/05/25/字符串分割性能对比/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="对于开发人员来说，字符串分割很常见。那么我们就来掰一下他的由来和应用场景，在前后端交互场景中，用户批量操作， 比如批量审批退款，批量删除数据等等，这对于程序来说就是一种批量操作，这种问题有两种解决方案：">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s7.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s8.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s9.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s10.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s11.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s12.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s13.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s14.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s15.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s16.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s17.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s18.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s19.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s20.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s21.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s22.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s23.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s24.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s25.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s26.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s27.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s28.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/result.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/test1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/test2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/test3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/test4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/h4.jpg">
<meta property="og:updated_time" content="2018-05-25T15:16:20.778Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="字符串分割性能对比">
<meta name="twitter:description" content="对于开发人员来说，字符串分割很常见。那么我们就来掰一下他的由来和应用场景，在前后端交互场景中，用户批量操作， 比如批量审批退款，批量删除数据等等，这对于程序来说就是一种批量操作，这种问题有两种解决方案：">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/05/25/字符串分割性能对比/s1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/05/25/字符串分割性能对比/" title="字符串分割性能对比" itemprop="url">字符串分割性能对比</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-05-25T12:41:20.000Z" itemprop="datePublished"> Published 2018-05-25</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#场景引入"><span class="toc-number">1.</span> <span class="toc-text">场景引入</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题引出"><span class="toc-number">2.</span> <span class="toc-text">问题引出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">3.</span> <span class="toc-text">解决方案</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#方式一-String的split解析"><span class="toc-number">3.1.</span> <span class="toc-text">方式一: String的split解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式二-StringTokenizer解析"><span class="toc-number">3.2.</span> <span class="toc-text">方式二: StringTokenizer解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式三-Pattern解析"><span class="toc-number">3.3.</span> <span class="toc-text">方式三: Pattern解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式四-substring解析"><span class="toc-number">3.4.</span> <span class="toc-text">方式四: substring解析</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#对比性能"><span class="toc-number">4.</span> <span class="toc-text">对比性能</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#总结"><span class="toc-number">4.1.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#附上本人公众号"><span class="toc-number">4.2.</span> <span class="toc-text">附上本人公众号</span></a></li></ol></li></ol>
		
		</div>
		
		<pre><code>    对于开发人员来说，字符串分割很常见。那么我们就来掰一下他的由来和应用场景，在前后端交互场景中，用户批量操作，
比如批量审批退款，批量删除数据等等，这对于程序来说就是一种批量操作，这种问题有两种解决方案：
</code></pre><a id="more"></a>
<pre><code>1）前端循环调后端接口
2）前端调用后端批量操作接口
    显然第一种是不可取的，在大体量系统和网站中完全是不可行的，因为循环调用会增加前后端交互次数，网络环境不好时丢包、调用失败的
概率成倍增加，并且增加服务器压力。
    我们一般会采用第二种方式来实现批量操作，但是调用批量接口根据不同的业务场景也有两种常用的实现方式：
I 前端将完整的数据信息传给后端
II前端将关键标识传给后端
    当然，第一种方式在批量新增数据的场景用的比较多，有一个缺点是依赖带宽和后端程序能够接收的数据大小阈值；第二种在查询更新和删除
数据的场景下用的比较多，比如我根据会员id列表查询会员信息，批量审批退款以及批量删除无用数据，这时我们只需要将关键信息(比如id)用
特殊字符隔开形成字符串传给后端，同样也可以使用I方式，将完整数据信息传送给后端，但是这种场景下完全没必要，并且服务端还需要反序列化
成原来的数据样式，性能当然没有原生的plaint text好。
    那么接下来，我们将对于业务场景中的前端传送给后端的关键信息形成的字符串分割成我们能够应用的关键字列表做详细分析。
</code></pre><h5 id="场景引入"><a href="#场景引入" class="headerlink" title="场景引入"></a>场景引入</h5><pre><code>     对于逆向交易的审批退款操作，有时候需要批量审批退款，这时候前端调用后端批量审批接口并将退款编号用逗号隔开传输参数，
服务端的做法是先将字符串分割解析成退款编号列表，然后再调用真实的退款接口。
</code></pre><h5 id="问题引出"><a href="#问题引出" class="headerlink" title="问题引出"></a>问题引出</h5><pre><code>    对于将前端传来的字符串(格式&quot;123,456,789......&quot;)解析成程序能够识别的退款编号列表，有不同的实现方式，那么解析性能的好坏将直接影响到
用户体验，那么我们如何解析才能保证性能呢？
</code></pre><h5 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h5><pre><code>    根据个人编码经验以及参阅资料，解析字符串至少有一下四种方式：
1）使用String自带split方法分割字符串并解析
2）使用StringTokenizer分割并解析
3）使用Pattern正则表达式分割解析
4）使用substring分割解析
那么哪一种方式性能更好？接下来将一一实现和分析对比
</code></pre><h6 id="方式一-String的split解析"><a href="#方式一-String的split解析" class="headerlink" title="方式一: String的split解析"></a>方式一: String的split解析</h6><pre><code>使用String中的split方法将字符串分割成数组然后转换成列表
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 使用string.split分解字符串并转换成list</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-05-18 22:14 Friday</div><div class="line"> * @param str</div><div class="line"> * @param delimiter</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private static List&lt;Long&gt; splitStr2List(String str,String delimiter) &#123;</div><div class="line">	if(null == str || str.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	String[] arr = str.split(delimiter);</div><div class="line">	List&lt;Long&gt; list = new ArrayList&lt;&gt;(arr.length);</div><div class="line">	int i = 0;</div><div class="line">	String temp;</div><div class="line">	do&#123;</div><div class="line">		temp = arr[i];</div><div class="line">		if(!isNumber(temp)) &#123;</div><div class="line">			continue;</div><div class="line">		&#125;</div><div class="line">		list.add(Long.valueOf(temp));</div><div class="line">	&#125; while(++ i &lt; arr.length);</div><div class="line">	</div><div class="line">	return list;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h6 id="方式二-StringTokenizer解析"><a href="#方式二-StringTokenizer解析" class="headerlink" title="方式二: StringTokenizer解析"></a>方式二: StringTokenizer解析</h6><pre><code>实现代码：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 使用StringTokenizer分割字符串并转换成list</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-05-18 22:07 Friday</div><div class="line"> * @param str</div><div class="line"> * @param delimiter</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private static List&lt;Long&gt; transferStr2List(String str,String delimiter) &#123;</div><div class="line">	if(null == str || str.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	StringTokenizer tokenizer = new StringTokenizer(str, DEFAULT_DELIMITER);</div><div class="line">	List&lt;Long&gt; list = new ArrayList&lt;&gt;(tokenizer.countTokens());</div><div class="line">	String temp;</div><div class="line">	while(tokenizer.hasMoreTokens()) &#123;</div><div class="line">		temp = tokenizer.nextToken();</div><div class="line">		if(!isNumber(temp)) &#123;</div><div class="line">			continue;</div><div class="line">		&#125;</div><div class="line">		list.add(Long.valueOf(temp));</div><div class="line">	&#125;</div><div class="line">	return list;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h6 id="方式三-Pattern解析"><a href="#方式三-Pattern解析" class="headerlink" title="方式三: Pattern解析"></a>方式三: Pattern解析</h6><pre><code>Pattern正则表达式将字符串按照传入表达式分割:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 使用Pattern分割字符串并转换成list</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-05-18 22:19 Friday</div><div class="line"> * @param str</div><div class="line"> * @param delimiter</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private static List&lt;Long&gt; patternStr2List(String str,String delimiter) &#123;</div><div class="line">	if(null == str || str.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	Pattern pattern = Pattern.compile(delimiter);</div><div class="line">	String[] arr = pattern.split(str);</div><div class="line">	List&lt;Long&gt; list = new ArrayList&lt;&gt;(arr.length);</div><div class="line">	int i = 0;</div><div class="line">	String temp;</div><div class="line">	do&#123;</div><div class="line">		temp = arr[i];</div><div class="line">		if(!isNumber(temp)) &#123;</div><div class="line">			continue;</div><div class="line">		&#125;</div><div class="line">		list.add(Long.valueOf(temp));</div><div class="line">	&#125; while(++i &lt; arr.length);</div><div class="line">	</div><div class="line">	return list;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h6 id="方式四-substring解析"><a href="#方式四-substring解析" class="headerlink" title="方式四: substring解析"></a>方式四: substring解析</h6><pre><code>使用String自带的substring方法，将字符串分割成子字符串的方式分割:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 使用substring方式分割字符串并转换成list</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-05-18 22:45 Friday</div><div class="line"> * @param str</div><div class="line"> * @param delimiter</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private static List&lt;Long&gt; substring2List(String str,String delimiter) &#123;</div><div class="line">	//12,33,45</div><div class="line">	if(null == str || str.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	assert null != delimiter &amp;&amp; str.length()&gt;= delimiter.length();</div><div class="line">	int idx = 0;</div><div class="line">	String temp;</div><div class="line">	int deliLength = delimiter.length();</div><div class="line">	List&lt;Long&gt; list = new ArrayList&lt;&gt;();</div><div class="line">	for(int i = 0,length = str.length();i &lt; length; i ++) &#123;</div><div class="line">		String del = str.substring(i, i + deliLength);</div><div class="line">		if(!del.equals(delimiter)) &#123;</div><div class="line">			continue;</div><div class="line">		&#125;</div><div class="line">		temp = str.substring(idx,i);</div><div class="line">		if(!isNumber(temp)) &#123;</div><div class="line">			continue;</div><div class="line">		&#125;</div><div class="line">		list.add(Long.valueOf(temp));</div><div class="line">		idx += temp.length() + deliLength;</div><div class="line">	&#125;</div><div class="line">	return list;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h5 id="对比性能"><a href="#对比性能" class="headerlink" title="对比性能"></a>对比性能</h5><pre><code>    上边我们已经用四种方式实现了将字符串分割解析城Long类型列表，那么每种方式的具体性能如何，我们编写测试程序可以对比一下：
基础指标，我们将字符串包含的Long数据个数设置为10,100,1000,10000,100000,1000000,5000000(太大的话程序运行导致堆内存溢出并且没有业务场景)
1）string的split方式:
长度10
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s1.png" alt="string" title="string">
<pre><code>长度100
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s2.png" alt="string" title="string">
<pre><code>长度1000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s3.png" alt="string" title="string">
<pre><code>长度10000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s4.png" alt="string" title="string">
<pre><code>长度100000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s5.png" alt="string" title="string">
<pre><code>长度1000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s6.png" alt="string" title="string">
<pre><code>长度5000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s7.png" alt="string" title="string">
<pre><code>2）StringTokenizer方式
长度10
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s8.png" alt="string" title="string">
<pre><code>长度100
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s9.png" alt="string" title="string">
<pre><code>长度1000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s10.png" alt="string" title="string">
<pre><code>长度10000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s11.png" alt="string" title="string">
<pre><code>长度100000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s12.png" alt="string" title="string">
<pre><code>长度1000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s13.png" alt="string" title="string">
<pre><code>长度5000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s14.png" alt="string" title="string">
<pre><code>3）Pattern方式
长度10
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s15.png" alt="string" title="string">
<pre><code>长度100
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s16.png" alt="string" title="string">
<pre><code>长度1000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s17.png" alt="string" title="string">
<pre><code>长度10000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s18.png" alt="string" title="string">
<pre><code>长度100000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s19.png" alt="string" title="string">
<pre><code>长度1000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s20.png" alt="string" title="string">
<pre><code>长度5000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s21.png" alt="string" title="string">
<pre><code>4）substring方式
长度10
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s22.png" alt="string" title="string">
<pre><code>长度100
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s23.png" alt="string" title="string">
<pre><code>长度1000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s24.png" alt="string" title="string">
<pre><code>长度10000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s25.png" alt="string" title="string">
<pre><code>长度100000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s26.png" alt="string" title="string">
<pre><code>长度1000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s27.png" alt="string" title="string">
<pre><code>长度5000000
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/s28.png" alt="string" title="string">
<pre><code>上面我们把每一种方式按照给定指标都跑了测试，下图是我们整理出来的性能测试结果：
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/result.png" alt="string" title="string">
<pre><code>注意:时间单位 毫秒

    可以看出在数据量不是很大的情况下，StringTokenizer分割字符串性能应该是最好的，其他的性能差别也都不太大。
但是话有说回来，后端服务器会接收前端传来长度百万级别以上的数据吗？有前端会像后端发送长度百万级别以上的数据吗？
写此篇文章的目的，是对几种常见的分割字符串的方式做一下讲解和性能对比，具体应用在什么样的场景适用怎么样的方式
还是取决于开发人员。
    对于四种分割字符串的实现方式和性能对比做了一番描述，但是有没有人发现上述几种实现方式代码层面的返回结果都写死
成List&lt;Long&gt;类型，那么如果我想解析成Integer是不是还要重写一份代码？当然作为一个有经验的程序员，你写代码的时候一定
会考虑代码复用性和封装等等，那么此处我们想把字符串解析成通用的数据类型列表，显然要引入泛型，看一下实现方案：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 将字符串解析成指定的数据类型列表</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-05-25 23:01 Friday</div><div class="line"> * @param str</div><div class="line"> * @param delimiter</div><div class="line"> * @param clazz</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">public static &lt;T&gt; List&lt;T&gt; transfer2ListV2(String str,String delimiter,Class&lt;T&gt; clazz) &#123;</div><div class="line">	if(null == str || str.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	String[] arr = str.split(delimiter);</div><div class="line">	List&lt;T&gt; list = new ArrayList&lt;&gt;(arr.length);</div><div class="line">	for(int i = 0,length = arr.length; i &lt; length ; i ++) &#123;</div><div class="line">		String temp = arr[i];</div><div class="line">		if(clazz.isAssignableFrom(Long.class)) &#123;//long类型</div><div class="line">			Long element = Long.valueOf(temp);</div><div class="line">			list.add((T)element);</div><div class="line">		&#125; else if(clazz.isAssignableFrom(Integer.class)) &#123;</div><div class="line">			Integer ele = Integer.valueOf(temp);</div><div class="line">			list.add((T)ele);</div><div class="line">		&#125; else if(clazz.isAssignableFrom(String.class)) &#123;</div><div class="line">			list.add((T)temp);</div><div class="line">		&#125; else &#123;</div><div class="line">			throw new IllegalArgumentException(&quot;转换类型不支持&quot;);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	return list;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    这里边有一个点需要解释一下，为什么对泛型T类型的判断只有Long、Integer和String呢？因为，首先我们分隔出来的具体每一个子项是有
具体的业务含义的，比如会员id，退款编号等等，这些业务数据类型有一个很明显的特点，标识性特别强，基本不会重复，那么我们常用的数据
类型那些可以描述标识性比较强的数据呢，显然Long，Integer和String类型，Boolean类型识别性太差，传过来true和false有什么意义呢。

   接下来使用三种类型的数据简单跑一下测试：
1）Long类型
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/test1.png" alt="string" title="string">
<pre><code>2）Integer类型
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/test2.png" alt="string" title="string">
<pre><code>3）String类型
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/test3.png" alt="string" title="string">
<pre><code>我们使用错误的转换类型跑一下
</code></pre><img src="/typhoon/2018/05/25/字符串分割性能对比/test4.png" alt="string" title="string">
<h6 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h6><pre><code>    经过上述的描述和代码验证，我们对几种常见的字符串分割解析方式做了详细的介绍的性能对比，以及最后对解析方式做了泛化来提高代码的复用性，
希望给大家在日常开发中带来帮助！
</code></pre><p><strong><em>PS:原创不易,多多支持!</em></strong></p>
<h6 id="附上本人公众号"><a href="#附上本人公众号" class="headerlink" title="附上本人公众号"></a>附上本人公众号</h6><img src="/typhoon/2018/05/25/字符串分割性能对比/h4.jpg" alt="string" title="string">  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/java性能优化/">java性能优化</a>
</div>


</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/05/25/字符串分割性能对比/" data-title="字符串分割性能对比 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/05/26/jdbcTemplate实现分页/" title="jdbcTemplate实现物理分页">
  <strong>上一篇：</strong><br/>
  <span>
  jdbcTemplate实现物理分页</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/05/20/dubbo富客户端/"  title="dubbo富客户端">
 <strong>下一篇：</strong><br/> 
 <span>dubbo富客户端
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#场景引入"><span class="toc-number">1.</span> <span class="toc-text">场景引入</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题引出"><span class="toc-number">2.</span> <span class="toc-text">问题引出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">3.</span> <span class="toc-text">解决方案</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#方式一-String的split解析"><span class="toc-number">3.1.</span> <span class="toc-text">方式一: String的split解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式二-StringTokenizer解析"><span class="toc-number">3.2.</span> <span class="toc-text">方式二: StringTokenizer解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式三-Pattern解析"><span class="toc-number">3.3.</span> <span class="toc-text">方式三: Pattern解析</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#方式四-substring解析"><span class="toc-number">3.4.</span> <span class="toc-text">方式四: substring解析</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#对比性能"><span class="toc-number">4.</span> <span class="toc-text">对比性能</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#总结"><span class="toc-number">4.1.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#附上本人公众号"><span class="toc-number">4.2.</span> <span class="toc-text">附上本人公众号</span></a></li></ol></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
